package com.ruowei.modules.webSocket.web.push;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.ApacheHttpClient;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.ruowei.config.ApplicationProperties;
import com.ruowei.modules.webSocket.domain.Message;
import com.ruowei.modules.webSocket.domain.enumeration.ReceiverType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class JPushUtil {

    private static ApplicationProperties applicationProperties;

    public JPushUtil(ApplicationProperties applicationProperties) {
        JPushUtil.applicationProperties = applicationProperties;
    }

    public static PushResult commonPush(Message message, ReceiverType receiverType) {
        ClientConfig clientConfig = ClientConfig.getInstance();
        final JPushClient jPushClient = getClient(receiverType, clientConfig);
        if (jPushClient == null) {
            return null;
        }
        String authCode = getAuthCode(receiverType);
        ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
        jPushClient.getPushClient().setHttpClient(httpClient);

        PushPayload pushPayload;

        if (message.isJumpPage()) {
            Map<String, String> extras = message.getCustomMessage();
            pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.alias(message.getReceiverCode()))
                .setMessage(cn.jpush.api.push.model.Message
                    .newBuilder()
                    .setTitle(message.getTitle())
                    .setMsgContent(message.getContent())
                    .addExtras(extras)
                    .build())
//                .setNotification(Notification.android(message.getContent(), message.getTitle(), extras))
                .build();
        } else {
            pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.alias(message.getReceiverCode()))
                .setMessage(cn.jpush.api.push.model.Message
                    .newBuilder()
                    .setTitle(message.getTitle())
                    .setMsgContent(message.getContent())
                    .build())
//                .setNotification(Notification.alert(message.getContent()))
                .build();
        }
        try {
            return jPushClient.sendPush(pushPayload);
        } catch (APIConnectionException | APIRequestException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getAuthCode(ReceiverType receiverType) {
        switch (receiverType) {
            case MERCHANT_APP: {
                return ServiceHelper.getBasicAuthorization(applicationProperties.getMerchantAppKey(), applicationProperties.getMerchantAppSecret());
            }
            case COURIER_APP: {
                return ServiceHelper.getBasicAuthorization(applicationProperties.getCourierAppKey(), applicationProperties.getCourierAppSecret());
            }
            case CAR_APP: {
                return ServiceHelper.getBasicAuthorization(applicationProperties.getCarAppKey(), applicationProperties.getCarAppSecret());
            }
        }
        return null;
    }

    private static JPushClient getClient(ReceiverType receiverType, ClientConfig clientConfig) {
        switch (receiverType) {
            case MERCHANT_APP: {
                return new JPushClient(applicationProperties.getMerchantAppSecret(), applicationProperties.getMerchantAppKey(), null, clientConfig);
            }
            case COURIER_APP: {
                return new JPushClient(applicationProperties.getCourierAppSecret(), applicationProperties.getCourierAppKey(), null, clientConfig);
            }
            case CAR_APP: {
                return new JPushClient(applicationProperties.getCarAppSecret(), applicationProperties.getCarAppKey(), null, clientConfig);
            }
        }
        return null;
    }
}
